<?php

namespace Admin\Controller;

use Common\Controller\AdminbaseController;

class TeampaisysController extends AdminbaseController {
    public function index() {
        
        $p=1;
        if (isset($_GET['p'])){
            $p=$_GET['p'];
        }
        $queryDesc='order by fen desc';
        
        $postData=array();
        $where='';
        $postData['p']=$p;
        
        $Model = M();
        if (isset($_GET['gid'])){
            $_SESSION['gameid']=$_GET['gid'];
        }
        $gameInfo=$Model->query("SELECT id,name FROM `z_game`   ");
        if (isset($_SESSION['gameid'])){
            $queryTable=$Model->query("SELECT id,name FROM `z_game` where id=".$_SESSION['gameid']." limit 0,1 ");
            $tableName='y'.$queryTable[0]['id'];
        }else{
            $queryTable=$Model->query("SELECT id,name FROM `z_game` where is_open=1 limit 0,1 ");
            $tableName='y'.$queryTable[0]['id'];
        }
        $gameId=$queryTable[0]['id'];
        $gameName=$queryTable[0]['name'];

       
        if ($_POST['name']){
            $postData['name']=$_POST['name'];
            $name=addslashes(trim($_POST['name']));
            
            $where=" where name like '%".$name."%' ";
        }
        
        $result = $Model->query("SHOW TABLES LIKE '".$tableName."_team' ");
        if (empty($result)){
            $tableName='y1'; //表不存在
            $where=" where id='0' ";
        }
        
        
        $query=$Model->query("SELECT count(id) as count FROM `".$tableName."_team` ".$where."  ");
        $count=$query[0]['count'];
        $page = $this->page($count, 10, $p);
        $uInfo=$Model->query("SELECT * FROM `".$tableName."_team` ".$where."  ".$queryDesc." limit ".(($p-1)*10).",10 ");
        $theData=array();
        $pid=1 + (($p-1)*10);
        foreach ($uInfo as $v){
           
            $theData[]=array('id'=>$v['id'],'pid'=>$pid,'name'=>$v['name'],'fen'=>$v['fen'],'renshu'=>$v['renshu'],
                'ishuojiang'=>$v['ishuojiang']);
            $pid++;
        }
        $this->assign("postData", $postData);
        $this->assign("page", $page->show('Admin'));
        $this->assign("thedata", $theData);
        $this->assign("gameName", $gameName);
        $this->assign("gameId", $gameId);
        $this->assign("gameInfo", $gameInfo);
        
        $this->display();
    }
    
    public function add() {
        
        
        $this->display();
    }
    
    public function theget() {
      
        if (isset($_POST['id'])){
            $Model = M();
            
            $queryTable=$Model->query("SELECT id FROM `z_game` where is_open=1 limit 0,1 ");
            $tableName='y'.$queryTable[0]['id'];
            
            $uInfo=$Model->query("SELECT * FROM `".$tableName."_group` where tid='".$_POST['id']."'  ");
            $user=array();
            foreach ($uInfo as $v){
                $getPhone=$Model->query("SELECT phone FROM `".$tableName."_users` where openid='".$v['openid']."' limit 0,1 ");
                $phone='';
                if (isset($getPhone[0]['phone'])){
                    $phone=$getPhone[0]['phone'];
                }
                $getFen=$Model->query("SELECT fen FROM `".$tableName."_fen` where openid='".$v['openid']."' limit 0,1 ");
                $fen=0;
                if (isset($getFen[0]['fen'])){
                    $fen=$getFen[0]['fen'];
                }
                $user[]=array('name'=>base64_decode($v['nickname']),
                    'headimg'=>$v['headimg'],'phone'=>$phone,'fen'=>$fen
                );
            }
            $info=array('s'=>'y','u'=>$user);
            echo json_encode($info);
        }
        
        
    }
    
    public function edit() {
        if (isset($_GET['id'])){
            $id=$_GET['id'];
        }else{
            exit;
        }
        $p=1;
        if (isset($_GET['p'])){
            $p=$_GET['p'];
        }
        $Model = M();
        
        $queryTable=$Model->query("SELECT id FROM `z_game` where is_open=1 limit 0,1 ");
        $tableName='y'.$queryTable[0]['id'];
        
     
        $thedata=$Model->query("SELECT id,ishuojiang,name FROM `".$tableName."_team` where id='".$id."' limit 0,1  ");
        
        
        $this->assign("p", $p);
        $this->assign("thedata", $thedata[0]);
        
        $this->display();
    }
     
    
    public function editpost() {
        if (isset($_POST['ishuojiang']) && isset($_POST['option_id'])){
            
            $Model = M();
            $queryTable=$Model->query("SELECT id FROM `z_game` where is_open=1 limit 0,1 ");
            $tableName='y'.$queryTable[0]['id'];
            if (empty($_POST['ishuojiang'])){
                $Model->execute(" update `".$tableName."_team`  SET `ishuojiang`=NULL where id='".$_POST['option_id']."'  ");
            }else{
                $Model->execute(" update `".$tableName."_team`  SET `ishuojiang`=1  where id='".$_POST['option_id']."'  ");
            }
            $info=json_encode(array('status'=>'yes'));
            echo $info;
        }
    }
 
}
